package com.roy.netty.sworddemo.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class ClientSettings {
	private int transportBossThreads = 0 ;
    private boolean transportNativeEpoll=false;
    private List<InetSocketAddress> transportServers;
    private int transportConnectTimeoutMillis=2000;
    private boolean transportTcpNoDelay=true;
    private boolean transportSoKeepAlive = true;
    private boolean transportSoReuseAddress = true;
    private int transportSoBacklog = 131072;
    private int transportSoSendBufferSize = 131072;
    private int transportSoReceiveBufferSize = 131072;
    private long transportPoolAcquireTimeoutMillis = 2000;
    private int transportPoolMaxConnections = 200;
    private int transportPoolMaxPendingAcquires = 300;
    private int transportFetchWaitMilliSeconds = 6000;

    private int messageVersion = 0 ;
    private int messagePreLength = 4;
    private Charset messageEncoding = Charset.forName("UTF-8");

    public int getTransportBossThreads() {
        return transportBossThreads;
    }

    public void setTransportBossThreads(String transportBossThreads) {
        this.transportBossThreads = Integer.parseInt(transportBossThreads);
    }

    public boolean isTransportNativeEpoll() {
        boolean isLinux = System.getProperty("os.name").toLowerCase().startsWith("linux");
        return isLinux && transportNativeEpoll;
    }

    public void setTransportNativeEpoll(String transportNativeEpoll) {
        this.transportNativeEpoll = Boolean.parseBoolean(transportNativeEpoll);
    }

    public List<InetSocketAddress> getTransportServers() {
        return transportServers;
    }

    public void setTransportServers(String transportServers) {
        this.transportServers = parseLine(transportServers);
    }
    private List<InetSocketAddress> parseLine(String line) {
        List<InetSocketAddress> addresses = new ArrayList<>();
        if (line == null)
            return addresses;
        line = line.trim();
        if ("".equals(line))
            return addresses;
        String[] parts = line.split(",");
        if (parts.length < 1)
            return addresses;
        for (String hostAndPort : parts) {
            InetSocketAddress address = parseServerAddress(hostAndPort);
            if (address != null)
                addresses.add(address);
        }
        return addresses;
    }
    
    private InetSocketAddress parseServerAddress(String hostAndPort) {
        if (hostAndPort == null)
            return null;
        hostAndPort = hostAndPort.trim();
        if ("".equals(hostAndPort))
            return null;
        String[] parts = hostAndPort.split(":");
        if (parts.length < 1)
            return null;
        if (parts[0] == null || "".equals(parts[0]))
            return null;
        String host = parts[0];
        int port = 9866;
        if (parts.length >= 2 && parts[1] != null && !"".equals(parts[1]))
            port = Integer.parseInt(parts[1]);
        return new InetSocketAddress(host, port);
    }

    public int getTransportConnectTimeoutMillis() {
        return transportConnectTimeoutMillis;
    }

    public void setTransportConnectTimeoutMillis(String transportConnectTimeoutMillis) {
        this.transportConnectTimeoutMillis = Integer.parseInt(transportConnectTimeoutMillis);
    }

    public boolean isTransportTcpNoDelay() {
        return transportTcpNoDelay;
    }

    public void setTransportTcpNoDelay(String transportTcpNoDelay) {
        this.transportTcpNoDelay = Boolean.parseBoolean(transportTcpNoDelay);
    }

    public boolean isTransportSoKeepAlive() {
        return transportSoKeepAlive;
    }

    public void setTransportSoKeepAlive(String transportSoKeepAlive) {
        this.transportSoKeepAlive = Boolean.parseBoolean(transportSoKeepAlive);
    }

    public boolean isTransportSoReuseAddress() {
        return transportSoReuseAddress;
    }

    public void setTransportSoReuseAddress(String transportSoReuseAddress) {
        this.transportSoReuseAddress = Boolean.parseBoolean(transportSoReuseAddress);
    }

    public int getTransportSoBacklog() {
        return transportSoBacklog;
    }

    public void setTransportSoBacklog(String transportSoBacklog) {
        this.transportSoBacklog = Integer.parseInt(transportSoBacklog);
    }

    public int getTransportSoSendBufferSize() {
        return transportSoSendBufferSize;
    }

    public void setTransportSoSendBufferSize(String transportSoSendBufferSize) {
        this.transportSoSendBufferSize = Integer.parseInt(transportSoSendBufferSize);
    }

    public int getTransportSoReceiveBufferSize() {
        return transportSoReceiveBufferSize;
    }

    public void setTransportSoReceiveBufferSize(String transportSoReceiveBufferSize) {
        this.transportSoReceiveBufferSize = Integer.parseInt(transportSoReceiveBufferSize);
    }

    public long getTransportPoolAcquireTimeoutMillis() {
        return transportPoolAcquireTimeoutMillis;
    }

    public void setTransportPoolAcquireTimeoutMillis(String transportPoolAcquireTimeoutMillis) {
        this.transportPoolAcquireTimeoutMillis = Long.parseLong(transportPoolAcquireTimeoutMillis);
    }

    public int getTransportPoolMaxConnections() {
        return transportPoolMaxConnections;
    }

    public void setTransportPoolMaxConnections(String transportPoolMaxConnections) {
        this.transportPoolMaxConnections = Integer.parseInt(transportPoolMaxConnections);
    }

    public int getTransportPoolMaxPendingAcquires() {
        return transportPoolMaxPendingAcquires;
    }

    public void setTransportPoolMaxPendingAcquires(String transportPoolMaxPendingAcquires) {
        this.transportPoolMaxPendingAcquires = Integer.parseInt(transportPoolMaxPendingAcquires);
    }

    public int getTransportFetchWaitMilliSeconds() {
        return transportFetchWaitMilliSeconds;
    }

    public void setTransportFetchWaitMilliSeconds(String transportFetchWaitMilliSeconds) {
        this.transportFetchWaitMilliSeconds = Integer.parseInt(transportFetchWaitMilliSeconds);
    }

    public int getMessageVersion() {
        return messageVersion;
    }

    public void setMessageVersion(String messageVersion) {
        this.messageVersion = Integer.parseInt(messageVersion);
    }

//    public MessageFormat getMessageFormat() {
//        return messageFormat;
//    }
//
//    public void setMessageFormat(String messageFormat) {
//        this.messageFormat = MessageFormat.valueOf(messageFormat.toUpperCase());
//    }

    public int getMessagePreLength() {
        return messagePreLength;
    }

    public void setMessagePreLength(String messagePreLength) {
        this.messagePreLength = Integer.parseInt(messagePreLength);
    }

    public Charset getMessageEncoding() {
        return messageEncoding;
    }

    public void setMessageEncoding(String messageEncoding) {
        this.messageEncoding = Charset.forName(messageEncoding);
    }

    @Override
    public String toString() {
        return "ClientSettings{" +
                "transportBossThreads=" + transportBossThreads +
                ", transportNativeEpoll=" + transportNativeEpoll +
                ", transportServers='" + transportServers + '\'' +
                ", transportTcpNoDelay=" + transportTcpNoDelay +
                ", transportSoKeepAlive=" + transportSoKeepAlive +
                ", transportSoReuseAddress=" + transportSoReuseAddress +
                ", transportSoBacklog=" + transportSoBacklog +
                ", transportSoSendBufferSize=" + transportSoSendBufferSize +
                ", transportSoReceiveBufferSize=" + transportSoReceiveBufferSize +
                ", messageVersion=" + messageVersion +
                ", messagePreLength=" + messagePreLength +
                ", messageEncoding=" + messageEncoding +
                '}';
    }
}
